﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUSService;
using DTOService;

namespace QuanLyBangDia
{
    public partial class frmThemBD : Form
    {
        //Thien code
        MyService myService = new MyService();
		
        public frmThemBD()
        {
            InitializeComponent();
            this.Icon = QuanLyBangDia.Properties.Resources.HDD;
        }

        private void btnThemLoaiBangDia_Click(object sender, EventArgs e)
        {
            frmQuanLyLoaiBangDia frm = new frmQuanLyLoaiBangDia();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                LoadLoaiBangDia();
            }
        }

        private void btnThemSanPham_Click(object sender, EventArgs e)
        {
            frmQuanLyLoaiSanPham frm = new frmQuanLyLoaiSanPham();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                LoadLoaiSanPham();
            }
        }

        private void btnThemTinhTrang_Click(object sender, EventArgs e)
        {
            frmQuanLyTinhTrangBangDia frm = new frmQuanLyTinhTrangBangDia();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                LoadTinhTrang();
            }
        }

        private void btnHoanThanh_Click(object sender, EventArgs e)
        {
            try
            {
                String error = "";
                if (String.IsNullOrEmpty(txtTenBangDia.Text))
                { error += "Nhập Tên Băng Đĩa!\n"; }
                if (String.IsNullOrEmpty(cbLoaiBangDia.Text))
                { error += "Nhập Loại Băng Đĩa!\n"; }
                if (String.IsNullOrEmpty(txtGiaBangDia.Text))
                { error += "Nhập Giá Băng Đĩa!\n"; }
                if (String.IsNullOrEmpty(cbLoaiSanPham.Text))
                { error += "Nhập Loại Sản Phẩm!\n"; }
                if (String.IsNullOrEmpty(cbTinhTrang.Text))
                { error += "Nhập Tình Trạng!\n"; }
                if (String.IsNullOrEmpty(txtSoLuong.Text))
                { error += "Nhập Số Lượng!\n"; }
                if (error == "")
                {
                    BangDia bd=new BangDia();
                    bd.TenBD=txtTenBangDia.Text;
                    bd.GiaBD=float.Parse(txtGiaBangDia.Text);
                    bd.LoaiBD = int.Parse(cbLoaiBangDia.SelectedValue.ToString());
                    bd.LoaiSanPham=int.Parse(cbLoaiSanPham.SelectedValue.ToString());
                    bd.MoTa=txtMoTa.Text;
                    bd.SoLuong=int.Parse(txtSoLuong.Text);
                    bd.TinhTrang = int.Parse(cbTinhTrang.SelectedValue.ToString());
                    
                    bool kq = myService.InsertBangDia(bd);
                    if (kq == true)
                    { DevComponents.DotNetBar.MessageBoxEx.Show("Thêm thành công");}
                    else
                        DevComponents.DotNetBar.MessageBoxEx.Show("Thêm thất bại");
                }
                else
                    DevComponents.DotNetBar.MessageBoxEx.Show(error, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);                    
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void btnSoanLai_Click(object sender, EventArgs e)
        {
            txtGiaBangDia.Text = "";
            txtMoTa.Text = "";
            txtTenBangDia.Text = "";
            cbLoaiBangDia.SelectedIndex = -1;
            cbLoaiSanPham.SelectedIndex = -1;
            cbTinhTrang.SelectedIndex = -1;
        }

        private void frmThemBD_Load(object sender, EventArgs e)
        {
            LoadLoaiBangDia();
            LoadLoaiSanPham();
            LoadTinhTrang();
        }

        private void LoadTinhTrang()
        {
            TinhTrang[] arrTT = myService.SelectTinhTrang();
            cbTinhTrang.DataSource = arrTT;
            cbTinhTrang.DisplayMember = "TenTinhTrang";
            cbTinhTrang.ValueMember = "MaTinhTrang";
            cbTinhTrang.SelectedIndex = 1;
        }

        private void LoadLoaiSanPham()
        {
            LoaiSanPham[] arrLSP = myService.SelectLoaiSanPham();
            cbLoaiSanPham.DataSource = arrLSP;
            cbLoaiSanPham.DisplayMember = "TenLoaiSanPham";
            cbLoaiSanPham.ValueMember = "MaLoaiSanPham";
        }

        private void LoadLoaiBangDia()
        {
            LoaiBangDia[] arrLBD = myService.SelectLoaiBangDia();
            cbLoaiBangDia.DataSource = arrLBD;
            cbLoaiBangDia.DisplayMember = "TenLoaiBangDia";
            cbLoaiBangDia.ValueMember = "MaLoaiBangDia";
        }

        private void frmThemBD_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK; 
        }
    }
}
